﻿<?php
/*
 * Created on 2009/08/03
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class MaidCafe
{
	public static function checkUser($botObj, $tweet)
	{
		$sql = "
		SELECT name, screen_name, last_visit, number_vists
		FROM user
		WHERE user.screen_name = '" . $botObj->db->real_escape_string($tweet["username"]) . "'";

		$result = $botObj->db->query($sql);
		
		if(!$result || $result->num_rows == 0)
		{
			return false;
		}
		else 
		{
			return $result->fetch_assoc();
		}
	}

	public static function createUser($botObj, $tweet)
	{
		$sql = "
		INSERT INTO user (name, screen_name, last_visit, number_vists)
		VALUES (
		'" . $botObj->db->real_escape_string($tweet["display_name"]) . "',
		'" . $botObj->db->real_escape_string($tweet["username"]) . "',
		NOW(),
		1
		)
		";
		
		$botObj->db->query($sql);
		
		return array(
		'name' => $tweet["display_name"],
		'screen_name' => $tweet["username"],
		'last_visit' => false,
		'num_vists' => 1
		);
	}
	
	public static function checkItem($botObj, $item_name)
	{
				$sql = "
				SELECT id 
				FROM item 
				WHERE name = '" . $botObj->db->real_escape_string($item_name) . "'";
				
				$result = $botObj->db->query($sql);
				
				if($result && $result->num_rows == 1)
				{
					return true;
				}				
				else
				{
					return false;
				}
	}
	
	public static function getMatches($string, $condition)
	{
		mb_regex_encoding('UTF-8');
		mb_ereg_search_init($string, $condition);
		$result = mb_ereg_search();
		$return = mb_ereg_search_getregs();
		array_shift($return);
		return $return;
	}

	public static function execute($botObj, $tweet, $action_value)
	{
		$user_data = self::checkUser($botObj, $tweet);
		if(!$user_data)
		{
			$botObj->pluginResponse['tweet'] = "";
			$user_data = self::createUser($botObj, $tweet);
		}
		
		switch($action_value)
		{
			case "menu":
				$sql = "SELECT name FROM item ORDER BY `order` ASC";
				$result = $botObj->db->query($sql);
				while($item = $result->fetch_assoc())
				{
					$items[] = $item['name'];
				}
				
				$menu = implode(", ", $items);
				$botObj->pluginResponse['tweet'] .= $menu;
			break;
			case "menu_selection":
				list($item) = self::getMatches($tweet['message'], "飲み物は(.+)でお願いします");
				if(self::checkItem($botObj, $item))
				{
					$botObj->pluginResponse['tweet'] = str_replace('{{drink}}', $item, $botObj->pluginResponse['tweet']);
				}
				else
				{
					$botObj->pluginResponse['tweet'] = "「$item」" . $botObj->config['maid']['unknown_item'];
				}
			break;
			case "multiple_menu":
				list($order) = self::getMatches($tweet['message'], "私は(.+)でお願いします");
				if(strstr($order, "と"))
				{
					$items = explode("と", $order);
					foreach($items as $item)
					{
						if(!self::checkItem($botObj, $item))
						{
							$unknowns[] = $item;
						}
					}
					if(!empty($unknowns))
					{
						//$botObj->pluginResponse['tweet'] = str_replace(
					}
					else
					{
						
					}
				}
				else
				{
					$botObj->pluginResponse['tweet'] = $botObj->config['maid']['unknown_order'];
				}
			break;
		}
	}
}
?>
